package com.versiFramework.services{
    import com.versiFramework.core.IInitializeClass;
    import com.versiFramework.core.IVersiClass;
    import com.versiFramework.events.versiEvent;
    import com.versiFramework.versi;
    
    import flash.events.EventDispatcher;
    
    import mx.events.PropertyChangeEvent;
	import flash.events.Event;
    import mx.messaging.Consumer;
    import mx.messaging.channels.AMFChannel;
    import mx.messaging.channels.PollingChannel;
    import mx.messaging.events.ChannelEvent;
    import mx.messaging.events.MessageAckEvent;
    import mx.messaging.events.MessageEvent;
    import mx.messaging.events.MessageFaultEvent;
    import mx.messaging.messages.AcknowledgeMessage;
    import mx.messaging.messages.AsyncMessage;
    
    public class consumerService extends EventDispatcher  implements IVersiClass, IInitializeClass {
		private var _name:String;
		public  var consumer:Consumer;
		private var _channelset:channel;
		private var _subtopic:String;
		private var _selector:String;
		private var _resubscribeAttempts:int = 0;
		private var _autoStart:Boolean = true;
		private var _receiveMessage:Function;
		
		
		//* Properties *//
		public function get name():String
		{
			return _name;
		}
		public function set name(setVal:String):void
		{
			_name=setVal;
		}
		
		public function get subtopic():String
		{
			return _subtopic;
		}
		public function set subtopic(setVal:String):void
		{
			_subtopic=setVal;
		}
		
		public function get selector():String
		{
			return _selector;
		}
		public function set selector(setVal:String):void
		{
			_selector=setVal;
		}
		
		public function set resubscribeAttempts(setVal:int):void
		{
			_resubscribeAttempts=setVal;
		}
		
		public function get resubscribeAttempts():int
		{
			return _resubscribeAttempts
		}
		
		public function set channelset(setVal:channel):void {
			_channelset = setVal;
			_channelset.addEventListener(ChannelEvent.DISCONNECT,disconnected);
			_channelset.addEventListener(ChannelEvent.CONNECT,reconnect);
		}
		
		public function get channelset():channel {
			return _channelset;
		}
		
		public function set autoStart(setVal:Boolean):void
		{
			_autoStart=setVal;
		}
			
		[Bindable] 
		public function get autoStart():Boolean
		{
			return _autoStart;
		}

		
		public function set receiveMessage(setVal:Function):void
		{
			if (consumer)
			{
				consumer.removeEventListener(MessageEvent.MESSAGE, receiveMessage);
				if (setVal != null) consumer.addEventListener(MessageEvent.MESSAGE, setVal);
			}
			_receiveMessage=setVal;
		}
		
		public function get receiveMessage():Function
		{
			return _receiveMessage != null ? _receiveMessage : onMessage
		}
		
		//* Message systems *//		
		public function startconsumer():void
		{
			if (!consumer && channelset.channelset)
			{
				
				consumer = new Consumer();
				consumer.resubscribeAttempts = resubscribeAttempts;
				consumer.addEventListener(MessageEvent.MESSAGE, receiveMessage);
				consumer.channelSet = channelset.channelset; 
				consumer.destination = channelset.destination;
			
				if (selector && selector.length) consumer.selector=selector;
				if (subtopic && subtopic.length) consumer.subtopic=subtopic;
				consumer.subscribe();
			}
		}
		
		public function stopconsumer():void
		{
			disconnected(null)
		}
		
		protected function disconnected(evt:Event):void
		{
			if (consumer)
			{
				consumer.unsubscribe();
				consumer.disconnect();
				consumer.removeEventListener(MessageEvent.MESSAGE, receiveMessage);
				consumer = null;
			}
		}
		
		public function reconnect(evt:Event):void
		{
			if (! consumer)
			{
				startconsumer()
			}
			else 
			{
				disconnected(evt)
				startconsumer()
			}
			
		}	
		
		//* Default functions *//
		protected function onMessage(event:MessageEvent):void
		{ 	
			var msg:AsyncMessage = AsyncMessage(event.message);
			this.dispatchEvent(new versiEvent(event.type,msg));
		}
		
		public function onInitializeClass():void
		{
			if (autoStart) startconsumer()
		}
		
		
		//* Constructor *//
		public function consumerService()
		{
			super();
		}

	}

}